﻿using System;
using System.Collections.Generic;
using System.Text;

namespace designmodule.constructionmodule.Singleton
{
    public class Singleton
    {
        private static Singleton instance;

        private Singleton()
        {

        }

        public static Singleton GetInstance()
        {
            if (instance == null)
            {
                instance = new Singleton();
            }

            return instance;
        }
    }

    public sealed class SingletonSafeForThread
    {
        static SingletonSafeForThread instance = null;
        private static readonly object padlock = new object();

        private SingletonSafeForThread()
        {
        }

        public static SingletonSafeForThread Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new SingletonSafeForThread();
                    }
                }

                return instance;
            }
        }
    }

    public sealed class SingletondoubleSafeForThread
    {
        static SingletondoubleSafeForThread instance = null;
        private static readonly object padlock = new object();

        private SingletondoubleSafeForThread()
        {
        }

        public static SingletondoubleSafeForThread Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (padlock)
                    {
                        if (instance == null)
                        {
                            instance = new SingletondoubleSafeForThread();
                        }
                    }
                }
                return instance;
            }
        }
    }

    public sealed class SingletonForNetSafeReadOnly
    {
        //交由.net 自己静态 readonly  做安全
        private static readonly SingletonForNetSafeReadOnly instance = null;
        static SingletonForNetSafeReadOnly()
        {
            instance = new SingletonForNetSafeReadOnly();
        }
        private SingletonForNetSafeReadOnly()
        {
        }
        public static SingletonForNetSafeReadOnly Instance
        {
            get
            {
                return instance;
            }
        }
    }


    public sealed class SingletonLazy
    {
        
        private static readonly Lazy<SingletonLazy> lazy =
            new Lazy<SingletonLazy>(() => new SingletonLazy());

        public static SingletonLazy Instance { get { return lazy.Value; } }

        private SingletonLazy()
        {
        }
    }

}
